package com.hnevc.mallserver3.controller;


import com.hnevc.mallserver3.common.ResponseResult;
import com.hnevc.mallserver3.dto.CommentDTO;
import com.hnevc.mallserver3.dto.Page;
import com.hnevc.mallserver3.entity.Comment;
import com.hnevc.mallserver3.entity.Goods;
import com.hnevc.mallserver3.service.CommentService;
import com.hnevc.mallserver3.service.impl.CommentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
@Api(tags = "评论信息接口")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/add")
    @ApiOperation(value = "addComment", notes = "添加评论信息")

    public ResponseResult addComment(@RequestBody Comment comment) {
        commentService.save(comment);
        return ResponseResult.success(comment);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getCommentById", notes = "根据ID获取评论")

    public ResponseResult getCommentById(@PathVariable Long id) {
        Comment comment = commentService.getById(id);
        return ResponseResult.success(comment);
    }

    @GetMapping("/fulldetail/{id}")
    @ApiOperation(value = "getCommentFullDetailById", notes = "根据ID获取评论全部详情")
    public ResponseResult getCommentFullDetailById(@PathVariable Long id) {
        CommentDTO commentDTO = commentService.selectCommentById(id);
        return ResponseResult.success(commentDTO);
    }

    @GetMapping("/list")
    @ApiOperation(value = "listCommentes", notes = "获取评论列表")

    public ResponseResult listCommentes() {
        List<Comment> commentes = commentService.list();
        return ResponseResult.success(commentes);
    }

    @PostMapping("/listByPage/{id}")
    @ApiOperation(value = "listCommentesByPage", notes = "获取评论列表")
    public ResponseResult listCommentesByPage(@PathVariable("id") Long id, @RequestBody Page page) {
        if (id < 1) id = 1L;
        List<CommentDTO> commentes = commentService.listByPage(id, page);
        return ResponseResult.success(commentes);
    }

    @GetMapping("/listByGoodId/{id}")
    @ApiOperation(value = "listByGoodId", notes = "根据食物编号获取评论列表")

    public ResponseResult listCommentesByGoodsId(@PathVariable("id") Long id) {

        List<CommentDTO> commentes = commentService.selectCommentByGoodsId(id);
        return ResponseResult.success(commentes);
    }

    @PutMapping("/update")
    @ApiOperation(value = "updateComment", notes = "更新评论信息")

    public ResponseResult updateComment(@RequestBody Comment comment) {
        commentService.updateById(comment);
        return ResponseResult.success(null);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "deleteComment", notes = "删除评论信息根据ID")

    public ResponseResult deleteComment(@PathVariable Long id) {
        commentService.removeById(id);
        return ResponseResult.success(null);
    }
}